package com.jade.editor.object.ani;

import java.util.ArrayList;

import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Region;
import org.eclipse.wb.swt.SWTResourceManager;

import com.jade.editor.object.editors.nodes.NodeCollector;

public class Anisotropy {

	private AnisotropyModel _model = null;
	private Point _size = null;
	private Region _aniRegion = null;
	private NodeCollector _collector = null;
	
	private ArrayList<Point> _globalList = new ArrayList<Point>();
	
	public ArrayList<Point> getPointsIn(){
		return _globalList;
	}
	
	public String getAniName(){
		return _model.getAniName();
	}
	
	public Anisotropy(AnisotropyModel model, Point size, NodeCollector n){
		
		_model = model;
		_size = size;
		_collector = n;
		
		if (_model != null && _model.getSize() != null && n.checkNodes())
			initData();
	}
	
	public void setSize(Point size){
		_size = size;
		if (_model != null && _model.getSize() != null && _collector.checkNodes())
			initData();
	}
	
	public void reCulc(){
		if (_model != null && _model.getSize() != null && _collector.checkNodes())
			initData();
	}
	
	public void paint(GC gc){
		if (_model != null && _model.getSize() != null){
			
			int countX = _size.x / _model.getSize().x;
			int countY = _size.y / _model.getSize().y;
			
			Color tmp = gc.getForeground();
			gc.setForeground(SWTResourceManager.getColor(154,169,209));
			
			for(int i = 0, mx = 0; i <= countX; i++, mx += _model.getSize().x){
				for(int j = 0, my = 0; j <= countY; j++, my += _model.getSize().y){
					
					for(Point p : _model.getPointList())
						gc.drawPolygon(point(p.x + mx, p.y + my));
				
					for(Cortege c : _model.getCortegeList())
						c.paint(gc, mx, my);
				}
			}
			
			gc.setForeground(tmp);
		}
	}
	
	private void initData(){
		
		_aniRegion = new Region();
		_aniRegion.add(_collector.getCoordsData());
		
		_globalList.clear();
		
		int countX = _size.x / _model.getSize().x;
		int countY = _size.y / _model.getSize().y;
		

		for(int i = 0, mx = 0; i <= countX; i++, mx += _model.getSize().x){
			for(int j = 0, my = 0; j <= countY; j++, my += _model.getSize().y){
				
				
				for(Point p : _model.getPointList()){
					Point newp = new Point(p.x + mx, p.y + my);
					if(_aniRegion.contains(newp))
						_globalList.add(newp);
				}
				
				for(Cortege c : _model.getCortegeList()){
					for(Point p : c.getPointsList()){
						Point newp = new Point(p.x + mx, p.y + my);
						if(_aniRegion.contains(newp))
							_globalList.add(newp);
					}
				}
				
			}
		}
		
		for(int i = 0; i < _globalList.size(); i++)
			System.out.println(i + "\t" + _globalList.get(i).x + "\t" +  _globalList.get(i).y);
	}
	
	private int[] point(int x,int y){
		int poly[] = new int[8];
		int sp = 1;
		poly[0] = x - sp;
		poly[1] = y - sp;
		
		poly[2] = x + sp;
		poly[3] = y - sp;
		
		poly[4] = x + sp;
		poly[5] = y + sp;
		
		poly[6] = x - sp;
		poly[7] = y + sp;
		
		return poly;
	}
	
}
